<template>
    <div class="app-container calendar-list-container">

        <!-- 查询和其他操作 -->
        <div class="filter-container">
          <!--<el-input clearable class="filter-item" style="width: 200px;" placeholder="平台编号" v-model="listQuery.platformId"></el-input>
          <el-input clearable class="filter-item" style="width: 200px;" placeholder="活动名称" v-model="listQuery.activityName"></el-input>
          <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">查找</el-button>-->
          <el-button class="filter-item" type="primary" v-if="loveCalculationRecommendManager_btn_add" @click="handleCreate" icon="el-icon-plus">添加</el-button>
        </div>

        <!-- 查询结果 -->
        <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>
          <el-table-column align="center" min-width="150px" label="平台编号" prop="platformId">
          </el-table-column>   
          <el-table-column align="center" min-width="150px" label="姓名" prop="userName">
          </el-table-column>
          <el-table-column align="center" min-width="150px" label="头像" prop="avert">
          </el-table-column>       
          <el-table-column align="center" min-width="200px" label="排序号" prop="sort">
          </el-table-column>
          <el-table-column align="center" min-width="200px" label="微信号" prop="wx">
          </el-table-column>  
          <el-table-column align="center" min-width="200px" label="手机号" prop="phone">
          </el-table-column> 
          <el-table-column align="center" label="操作" width="150" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button  type="primary" size="mini" v-if="loveCalculationRecommendManager_btn_info"  @click="handleUpdate(scope.row)">详情</el-button>
                <el-button  type="primary" size="mini" v-if="loveCalculationRecommendManager_btn_delete" @click="handleDelete(scope.row)">删除</el-button>
              </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination-container">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page"
            :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        </div>

        <!-- 添加对话框 -->
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
          <el-form :rules="rules" ref="dataForm" :model="dataForm" status-icon label-position="left" label-width="100px" style='width: 400px; margin-left:50px;'>
            <el-form-item label="用户" prop="userId">
              <el-select v-model="dataForm.userId" placeholder="请选择"  @change="changeUser" :disabled="disabled">
                <el-option
                  v-for="item in userList"
                  :key="item.userId"
                  :label="item.username"
                  :value="item.userId">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="排序号" prop="sort"> 
              <el-input type="text" v-model="dataForm.sort" clearable=""></el-input>
            </el-form-item>
            <el-form-item label="微信号" prop="sort"> 
              <el-input type="text" v-model="dataForm.wx" clearable=""></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="sort"> 
              <el-input type="text" v-model="dataForm.phone" clearable=""></el-input>
            </el-form-item>
            <el-form-item label="简介" prop="sort"> 
              <el-input  type="textarea" :rows="2" placeholder="请输入内容" v-model="dataForm.intro" clearable=""></el-input>
            </el-form-item>          
            <el-form-item label="头像" prop="avert">
              <div  @click="upCover">
                <img v-if="dataForm.avert" :src="dataForm.avert">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </div>
              <input type="file" name="file" id="upload_cover" @change="uploadCover" accept="image/png,image/gif" style="display: none">
            </el-form-item>             
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button v-if="dialogStatus == 'create'" type="primary" @click="createData('dataForm')">确定</el-button>
            <el-button v-if="dialogStatus == 'update'" type="primary" @click="updateData('dataForm')">确定</el-button>
          </div>
        </el-dialog>
    </div>
</template>

<script>
import waves from '@/directive/waves' // 水波纹指令
import { page, delObj, add, queryUserUnderPlatform, putObj } from '@/api/cms/loveCalculation/recommend'
import { uploadImg } from '@/api/cms/society/index'
import { mapGetters } from 'vuex'
import { formatDate } from '@/utils/index'

export default {
  name: 'activity',
  directives: {
    waves
  },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        platformId: '',
        merchantId: ''
      },
      userList: [],
      list: [],
      total: 0,
      listLoading: false,
      dialogFormVisible: false,
      disabled: false,
      downloadLoading: false,
      loveCalculationRecommendManager_btn_info: false,
      loveCalculationRecommendManager_btn_delete: false,
      loveCalculationRecommendManager_btn_add: false,
      dataForm: {
        avert: '',
        userId: '',
        userName: '',
        sort: '',
        amt: '',
        phone: '',
        wx: '',
        intro: ''
      },
      textMap: {
        create: '创建',
        update: '编辑'
      },
      dialogStatus: '',
      rules: {
      },
      pickerStartDateBegin: {
        disabledDate: (time) => {
          return time.getTime() < (Date.now() - 24 * 60 * 60 * 1000)
        }
      },
      pickerEndDateAfter: {
        disabledDate: (time) => {
          return time.getTime() < this.dataForm.activityStartTime
        }
      }
    }
  },
  filters: {
    formatDate1(time) {
      var date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm')
    }
  },
  created() {
    this.getList()
    this.getUserList()
    this.loveCalculationRecommendManager_btn_info = this.elements['loveCalculationRecommendManager:btn_info']
    this.loveCalculationRecommendManager_btn_delete = this.elements['loveCalculationRecommendManager:btn_delete']
    this.loveCalculationRecommendManager_btn_add = this.elements['loveCalculationRecommendManager:btn_add']
  },
  computed: {
    ...mapGetters([
      'elements',
      'name'
    ])
  },

  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    getUserList() {
      var obj = { id: 1 }
      queryUserUnderPlatform(obj).then(response => {
        console.log('0000000000000000000000000000000000000000')
        console.log(response)
        this.userList = response.list
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    changeUser(val) {
      let obj = {}
      obj = this.userList.find((item) => {
        return item.userId === val
      })
      console.log('=======----------=====----------=========')
      console.log(obj.username)
      this.dataForm.userName = obj.username
    },
    upCover: function() {
      document.getElementById('upload_cover').click()
    },
    uploadCover(event) {
      if (event.target.files.length > 0) {
        this.file = event.target.files[0] // 提交的图片
        console.log(this.file)
        const param = new FormData() // 创建form对象
        param.append('file', this.file)
        uploadImg(param).then(response => {
          if (response.success) {
            this.dataForm.avert = response.data.filePath
            alert('ok')
          }
        })
      }
    },
    getList() {
      this.listLoading = true
      this.listQuery.platformId = this.platformId
      page(this.listQuery).then(response => {
        console.log(response)
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleUpdate(row) {
      this.dataForm = row
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.disabled = true
    },
    updateData(formName) {
      putObj(this.dataForm.id, this.dataForm).then(() => {
        this.$notify({
          title: '成功',
          message: '更新成功',
          type: 'success',
          duration: 2000
        })
        this.getList()
        this.dialogFormVisible = false
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将删除选中项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delObj(row.id).then(response => {
          console.log(response)
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        }).catch(() => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.$notify({
            title: '失败',
            message: '删除失败',
            type: 'faile',
            duration: 2000
          })
        })
      })
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.disabled = false
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData(formName) {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          add(this.dataForm).then(response => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleDetail(row) {

    },
    handleClose(row) {
    },
    resetForm() {
      this.dataForm = {
      }
    }
  }
}
</script>

<style>

</style>


